﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Common;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.Sys.Controllers
{
    public class Sys_DictionaryController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetExtendList(string search)
        {
            List<Sys_DictionaryExtend> dictionarys = new Sys_DictionaryBusiness(DBEnum.MySqlADMP).GetExtendList(search);
            return Json(dictionarys);
        }

        [HttpPost]
        public IActionResult Add(Sys_Dictionary dictionary)
        {
            try
            {
                Sys_DictionaryBusiness business = new Sys_DictionaryBusiness(DBEnum.MySqlADMP);
                if(business.IsExist(dictionary.DictCode))
                {
                    return Json(new { Success = false, Message = "本字典项已存在，请修改字典编码！！" });
                }
                #region 完善Sys_Dictionary
                dictionary.OrderNo = dictionary.DictCode;
                dictionary.IsDeleted = 0;
                dictionary.Creator = SessionUtility.GetBasicUserInfo().UserName;
                dictionary.CreateDate = DateTime.Now;
                #endregion
                bool success = business.Add(dictionary);
                if (success)
                {
                    return Json(new { Success = true, Message = "添加成功！" });
                }
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统字典项添加失败", "dictionary=" + JsonConvert.SerializeObject(dictionary), ex, "DapperCore");
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Edit(Sys_Dictionary dictionary)
        {
            try
            {
                Sys_DictionaryBusiness business = new Sys_DictionaryBusiness(DBEnum.MySqlADMP);
                if (!business.IsExist(dictionary.DictCode))
                {
                    return Json(new { Success = false, Message = "本字典项不存在，请刷新后重试！！" });
                }
                #region 完善Sys_Dictionary
                Sys_Dictionary old = business.Get(dictionary.DictCode);
                old.DictName = dictionary.DictName;
                old.DictValue = dictionary.DictValue;
                old.IsActive = dictionary.IsActive;
                old.ParentCode = dictionary.ParentCode;
                old.Remarks = dictionary.Remarks;
                old.Modifier = SessionUtility.GetBasicUserInfo().UserName;
                old.ModifyDate = DateTime.Now;
                #endregion
                bool success = business.Update(old);
                if (success)
                {
                    return Json(new { Success = true, Message = "修改成功！" });
                }
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统字典项修改失败", "dictionary=" + JsonConvert.SerializeObject(dictionary), ex, "DapperCore");
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Delete(string deleteCodes)
        {
            try
            {
                bool success = new Sys_DictionaryBusiness(DBEnum.MySqlADMP).Delete(deleteCodes, SessionUtility.GetBasicUserInfo().UserName);
                if (success)
                {
                    return Json(new { Success = true, Message = "删除成功！" });
                }
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统字典项删除失败", "deleteCodes=" + deleteCodes, ex, "DapperCore");
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
        }
    }
}